package com.gtxd.utils;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.gtxd.utils.xss.SQLFilter;

/**
 * 分页参数对象
 *
 * @author yangc
 */

public class PageParams {
	// 当前页码
	private int currentPage;
	// 每页条数
	private int pageSize;
	// 偏移量
	private int offset;
	// 排序字段
	private String sidx;
	// 排序类型（升序、降序）
	private boolean order;

	public PageParams() {
	}

	/**
	 * @param CurrentPage
	 * @param pageSize
	 * @param offset
	 * @param sidx
	 * @param order
	 */
	public PageParams(PageParams params) {

		super();
		if (params.getPageSize() == 0) {
			this.currentPage = -1;
			this.pageSize = -1;
		} else {
			this.currentPage = params.getCurrentPage();
			this.pageSize = params.getPageSize();
		}

		this.offset = (currentPage - 1) * pageSize;
		// 防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
		this.sidx = params.getSidx() == null ? "" : params.getSidx().toString();
		if (StringUtils.isNotBlank(this.sidx)) {
			this.sidx = SQLFilter.sqlInject(this.sidx);
		}
		this.order = params.getOrder();
	}

	public PageParams(Map<String, Object> params) {

		// 分页参数
		this.currentPage = params.get("currentPage") == null ? -1
				: Integer.valueOf(params.get("currentPage").toString());
		this.pageSize = params.get("pageSize") == null ? -1 : Integer.valueOf(params.get("pageSize").toString());
		this.offset = (currentPage - 1) * pageSize;

		// 防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
		this.sidx = params.get("sidx") == null ? "" : params.get("sidx").toString();
		if (!StringUtils.isEmpty(this.sidx)) {
			this.sidx = SQLFilter.sqlInject(this.sidx);
		}
		this.order = params.get("order").equals("asc") ? true : false;

	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the offset
	 */
	public int getOffset() {
		return offset;
	}

	/**
	 * @param offset
	 *            the offset to set
	 */
	public void setOffset(int offset) {
		this.offset = offset;
	}

	/**
	 * 排序字段
	 * 
	 * @return the sidx
	 */
	public String getSidx() {
		return sidx;
	}

	/**
	 * @param sidx
	 *            排序字段
	 */
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	/**
	 * @return the order
	 */
	public boolean getOrder() {
		return order;
	}

	/**
	 * @param order
	 *            the order to set
	 */
	public void setOrder(boolean order) {
		this.order = order;
	}

	public static void main(String[] args) {
		System.out.println((int) 3.0);
	}
}
